package com.synnex.mobile.servier.core.survey.dao.impl;

import com.synnex.mobile.generic.dao.impl.GenericHibernateDaoImpl;
import com.synnex.mobile.servier.core.survey.dao.UserAnswerDao;
import com.synnex.mobile.servier.core.survey.dto.UserAnswer;

import java.math.BigInteger;

/**
 * Created with IntelliJ IDEA.
 * User: kerryf
 * Date: 13-8-12
 * To change this template use File | Settings | File Templates.
 */
public class UserAnswerDaoImpl extends GenericHibernateDaoImpl<UserAnswer, Integer> implements UserAnswerDao {

    public void saveUserAnswer(UserAnswer answer) {
        super.save(answer);
    }

    public Integer countDoSurveyTotal(Integer qid) {
        String sql = "SELECT COUNT(u.s_id) FROM user_answer u WHERE u.q_id=:qid AND u.a_id IS NOT NULL GROUP BY u.s_id LIMIT 1";

        Object data = this.getSession().createSQLQuery(sql).setParameter("qid", qid).uniqueResult();
        if (data == null) {
            return 0;
        } else {
            BigInteger bg =  (BigInteger) data  ;
            return bg.intValue();
        }

    }

}
